<?php
/**
 * 会员控制器
 *
 * @package    app
 * @version    1.0
 * @author     Ray 33705910@qq.com
 * @license    MIT License
 * @copyright  2013 - 2015 Ray
 * @link       http://wangxiaolei.cn
 */

/**
 * 本控制器主要用于：
 * 1.
 * @package  app
 * @extends  Controller
 */
namespace admin;

class Controller_Member extends  Controller_BaseController {

    public function before(){
        parent::before();

        $params = array(
            'controller_name' => '微信公众号管理'
        );
        \View::set_global($params);
    }

	public function action_index(){
		$params = array(
  			'title' => '会员管理',
  			'menu' => 'member',
            'action_name' => '会员管理 》会员列表'
  		);

		$items =  \Model_Member::query();

		$count = $items->count();
        $config = array(
            'pagination_url' => "/admin/member",
            'total_items'    => $count,
            'per_page'       => \Input::get('count', 15),
            'uri_segment'    => "start",
            'show_first'     => true,
            'show_last'      => true,
            'name'           => 'bootstrap3_cn'
        );

        $pagination = new \Pagination($config);
        $params['pagination'] = $pagination;        
        $params['items'] = $items
                            ->rows_offset($pagination->offset)
                            ->rows_limit($pagination->per_page)
                            ->get();
		

		$params['levels'] = \Model_MemberLevel::getItems();

  		\View::set_global($params);
		$this->template->content = \View::forge("ace/member/index");
	}

	/**
	* 查看一个会员
	*
	* @return
	*/
	public function action_view($id){
		if( ! $id){
			if(\Input::is_ajax()){
				die(json_encode(array('status' => 'err', 'msg' => '无效的参数', 'errcode' => 10)));
			}
		}
		$member = \Model_Member::find($id);
		if( ! $member){
			if(\Input::is_ajax()){
				die(json_encode(array('status' => 'err', 'msg' => '无效的参数', 'errcode' => 11)));
			}
		}

		if(\Input::is_ajax()){
			$member->level;
			$member->people->birthday = date('Y-m-d', $member->people->birthday);
			die(json_encode(array('status' => 'succ', 'msg' => '', 'errcode' => 0, 'data' => $member->to_array())));
		}
	}

	/**
	* 新增一个会员
	*
	* @return
	*/
	public function action_create(){
		if(\Input::method() == 'POST'){
			$data = \Input::post();
			$data['is_delete'] = 0;

			$data['user_id'] = \Auth::create_user('member' . time(), time(), isset($data['email']) && $data['email'] ? $data['email'] : time() . '@test.com', 1);
			
			if($data['birthday']){
				$data['birthday'] = strtotime($data['birthday']);
			}

			\Model_People::forge($data)->save();

			$data['status'] = 'NONE';
			$teacher = \Model_Member::forge($data);

			if($teacher->save()){
				\Response::redirect('/admin/member');
			}
		}
	}

	/**
	* 编辑会员资料
	*
	* @param $id int 会员ID
	* @return
	*/
	public function action_edit($id){

		if( ! $id){
			if(\Input::is_ajax()){
				die(json_encode(array('status' => 'err', 'msg' => '无效的参数', 'errcode' => 10)));
			}
		}

		$data = \Input::post();

		$member = \Model_Member::find($id);
		if( ! $member){
			if(\Input::is_ajax()){
				die(json_encode(array('status' => 'err', 'msg' => '无效的参数', 'errcode' => 11)));
			}
		}

		if(isset($data['birthday'])){
			$data['birthday'] = strtotime($data['birthday']);
		}
		
		$people = \Model_People::find($member->user_id);
		$people->set($data);
		$people->save();

		if(isset($data['is_new'])){
			$data['is_new'] = (int)$data['is_new'];
		}
		$member->set($data);
		if($member->save()){
			if(\Input::is_ajax()){
				die(json_encode(array('status' => 'succ', 'msg' => '操作成功', 'errcode' => 0)));
			}
			\Response::redirect('/admin/member');
		}else{
			if(\Input::is_ajax()){
				die(json_encode(array('status' => 'err', 'msg' => '操作失败', 'errcode' => 20)));
			}
		}
	}

	/**
	* 删除一个会员
	*
	* @param $id int 会员ID
	* @return
	*/
	public function action_delete($id){
		if( ! $id){
			if(\Input::is_ajax()){
				die(json_encode(array('status' => 'err', 'msg' => '无效的参数', 'errcode' => 10)));
			}
		}

		$member = \Model_Member::find($id);
		if( ! $member){
			if(\Input::is_ajax()){
				die(json_encode(array('status' => 'err', 'msg' => '无效的参数', 'errcode' => 11)));
			}
		}

		$member->people->first_name = '';
		$member->people->last_name = '';
		$member->is_delete = true;
		if($member->save()){
			if(\Input::is_ajax()){
				die(json_encode(array('status' => 'succ', 'msg' => '', 'errcode' => 0)));
			}
		}else{
			if(\Input::is_ajax()){
				die(json_encode(array('status' => 'err', 'msg' => '操作失败', 'errcode' => 10)));
			}
		}
	}

	/**
	* 创建一个级别
	*
	* @return
	*/
	public function action_level_create(){
		if(\Input::method() == 'POST'){
			$level = \Model_MemberLevel::forge(\Input::post());
			if($level->save()){
				if(\Input::is_ajax()){
					die(json_encode(array('status' => 'succ', 'msg' => '', 'errcode' => 0, 'data' => $level)));
				}
			}else{
				if(\Input::is_ajax()){
					die(json_encode(array('status' => 'err', 'msg' => '操作失败', 'errcode' => 20)));
				}
			}
		}
		
	}

	/**
	* 删除一个级别
	*
	* @param $id int 级别ID
	* @return
	*/
	public function action_level_delete($id = 0){
		$level = \Model_MemberLevel::find($id);
		if( ! $level){
			if(\Input::is_ajax()){
				die(json_encode(array('status' => 'err', 'msg' => '无效的参数', 'errcode' => 10)));
			}
		}

		if($level->delete()){
			if(\Input::is_ajax()){
				die(json_encode(array('status' => 'succ', 'msg' => '', 'errcode' => 0)));
			}
		}else{
			if(\Input::is_ajax()){
				die(json_encode(array('status' => 'err', 'msg' => '操作失败', 'errcode' => 10)));
			}
		}
	}

	/**
	* 删除一个级别
	*
	* @param $id int 级别ID
	* @return
	*/
	public function action_level_edit($id = 0){
		$level = \Model_MemberLevel::find($id);
		if( ! $level){
			if(\Input::is_ajax()){
				die(json_encode(array('status' => 'err', 'msg' => '无效的参数', 'errcode' => 10)));
			}
		}

		$level->set(\Input::post());
		if($level->save()){
			if(\Input::is_ajax()){
				die(json_encode(array('status' => 'succ', 'msg' => '', 'errcode' => 0)));
			}
		}else{
			if(\Input::is_ajax()){
				die(json_encode(array('status' => 'err', 'msg' => '操作失败', 'errcode' => 10)));
			}
		}
	}

	public function action_test(){
		

		if(\Input::is_ajax()){
			$file_name = time() . '.xlsx';
			$objPHPExcel = new \PHPExcel();
			$excel = new \PHPExcel_Writer_Excel2007($objPHPExcel);
			$excel->save($file_name);

			die(json_encode(array('file' => \Config::get('base_url') . $file_name)));
		}

		$params = array(
  			'title' => '会员管理',
  			'menu' => ''
  		);

  		\View::set_global($params);
		$this->template->content = \View::forge("edu/member/test");
	}

}
